<?php

namespace app\admin\controller;
use think\Db;
use app\common\service\BalanceService;
class Trade extends Common{

	public function otc(){
		return $this->fetch();	
	}
	public function sell(){
		return $this->fetch();	
	}
	public function buy(){
		return $this->fetch();	
	}

	/*
	 */
	public function otcList(){
		$limit = request()->param('limit')?:20;
		$username   = request()->param('username');
		$to_username   = request()->param('to_username');
		$status   = request()->param('status');
		$coin_name   = request()->param('coin_name');
		$where      = '1=1';
		if ($username != '') {
            $where .= ' and u1.username like \'%' . $username . '%\'';
        }
        if ($to_username != '') {
            $where .= ' and u2.username like \'%' . $to_username . '%\'';
        }
        if($status != '') {
            $where .= ' and t.status='.$status;
        }
        if ($coin_name != '') {
            $where .= ' and c.name like \'%' . $coin_name . '%\'';
        }
		$data  = Db::table('tp_trade_record')
				->alias('t')
				->join('tp_user u1','u1.id = t.sell_uid','LEFT')
				->join('tp_user u2','u2.id = t.buy_uid','LEFT')
				->join('tp_coin c','c.id = t.cid','LEFT')
				->where($where)
				->order('t.id desc')
				->field('t.*,u1.username,u2.username to_username,c.name coin_name')
				->paginate($limit);
		$data = $data->toArray();
		return $this->tableData($data);
	}


	/*
	otc 卖单
	*/
	public function sellList(){
		$limit = request()->param('limit')?:20;
		$username  = request()->param('username');
		$coin_name  = request()->param('coin_name');
		$uid   = request()->param('uid');
		$status   = request()->param('status');
		$where      = '1=1';
		if ($username != '') {
            $where .= ' and u.username like \'%' . $username . '%\'';
        }
        if ($coin_name != '') {
            $where .= ' and c.name like \'%' . $coin_name . '%\'';
        }
        if($uid != '') {
            $where .= ' and t.uid='.$uid;
        }
        if($status != '') {
            $where .= ' and t.status='.$status;
        }
		$data  = Db::table('tp_trade_list')
				->alias('t')
				->where('t.type',1)
				->join('tp_user u','u.id = t.uid','LEFT')
				->join('tp_coin c','c.id = t.cid','LEFT')
				->where($where)
				->order('status asc')
				->field('t.*,u.username,c.name coin_name')
				->paginate($limit);
		$data = $data->toArray();
		return $this->tableData($data);
	}


	/*
	otc 买单
	*/
	public function buyList(){
		$limit = request()->param('limit')?:20;
		$username  = request()->param('username');
		$coin_name  = request()->param('coin_name');
		$uid   = request()->param('uid');
		$status   = request()->param('status');
		$where      = '1=1';
		if ($username != '') {
            $where .= ' and u.username like \'%' . $username . '%\'';
        }
        if ($coin_name != '') {
            $where .= ' and c.name like \'%' . $coin_name . '%\'';
        }
        if($uid != '') {
            $where .= ' and t.uid='.$uid;
        }
        if($status != '') {
            $where .= ' and t.status='.$status;
        }
		$data  = Db::table('tp_trade_list')
				->alias('t')
				->where('t.type',0) 
				->join('tp_user u','u.id = t.uid','LEFT')
				->join('tp_coin c','c.id = t.cid','LEFT')
				->where($where)
				->order('status desc')
				->field('t.*,u.username,c.name coin_name')
				->paginate($limit);
		$data = $data->toArray();
		return $this->tableData($data);
	}

	public function complete(){
		$data = [];
		$data['id'] = request()->param('id');
		$info = Db::table('tp_trade_record')->where('id',$data['id'])->find();
		$data['uid'] = $info['sell_uid'];
		//$post_data = json_encode($data);
		$post_data = $data;
		$url = C('system_url').'/api/admin/confirmTrade?auth=asdfghjkl';
		$res = curl($url,$post_data,'post');
		$res = json_decode($res,true);
      
		if($res['code']==0){
			return ['code'=>0,'msg'=>$res['msg']];
		} else if ($res['code']==0) {
			return ['code'=>1,'msg'=>$res['msg']];
		}


		// $id = request()->param('id');
		// $info = Db::table('tp_trade_record')->where('id',$id)->find();
		// if($info['status'] == 3 || $info['status'] == 4){
		// 	return ['code'=>0,'msg'=>'订单状态错误'];
		// }
		// Db::startTrans();
		// $res1 = Db::table('tp_trade_record')->where('id',$id)->update(['status'=>3]);
		// $res2 = $this->setBalanceRecord($info['buy_uid'],$info['cid'],$info['amount'],$remark='交易买入',33);
		// $res3 = $this->updateBalance($info['buy_uid'],$info['cid'],$info['amount']);
		// if ($res1 && $res2 && $res3) {
		// 		Db::commit();
		// 		return ['code'=>1,'msg'=>'操作成功'];
		// 	} else {
		// 		Db::rollback();
		// 		return ['code'=>0,'msg'=>'操作失败'];
		// }	
	}

	public function cancel(){
		$id = request()->param('id');
		$info = Db::table('tp_trade_record')->where('id',$id)->find();
		if($info['status'] == 3 || $info['status'] == 4){
			return ['code'=>0,'msg'=>'订单状态错误'];
		}
		Db::startTrans();
		$res1 = Db::table('tp_trade_record')->where('id',$id)->update(['status'=>4]);
		$order = Db::table('tp_trade_list')->where('id',$info['order_id'])->find();
		if($order['status'] == 1){
			$left_amount = $order['left_amount'] + $info['amount'];
			$res2 = Db::table('tp_trade_list')->where('id',$info['order_id'])->update(['left_amount'=>$left_amount]);
		} elseif ($order['status'] == 2) {
			$res2 = Db::table('tp_trade_list')->where('id',$info['order_id'])->update(['left_amount'=>$info['amount'],'status'=>1]);
		} elseif ($order['status'] == 3) {
			$res2 = Db::table('tp_trade_list')->where('id',$info['order_id'])->update(['left_amount'=>$info['amount'],'status'=>1]);
		}
		//不退回手续费
		if($order['type'] == 1){
			$res3 = $res4 =1;
		} elseif ($order['type'] == 0) {
			$res3 = BalanceService::updateBalance($info['sell_uid'],$info['cid'],$info['amount'],'卖出取消退回',64);
			$res4 = BalanceService::updateBalance($info['sell_uid'],$info['cid'],$info['fee'],'卖出取消退回手续费',65);	
		}
		if ($res1 && $res2 && $res3 && $res4) {
				Db::commit();
				return ['code'=>0,'msg'=>'操作成功'];
			} else {
				Db::rollback();
				return ['code'=>1,'msg'=>'操作失败'];
		}
	}
  	public function cancel2(){
		if (request()->method() == 'POST') {
			$data = [];
			$data['id'] = request()->param('id');
			$data['uid'] = request()->param('uid');
			//$post_data = json_encode($data);
			$post_data = $data;
			$url = C('system_url').'/api/admin/undoOrder?auth=asdfghjkl';
			$res = curl($url,$post_data,'post');
			$res = json_decode($res,true);
          
			if($res['code']==0){
				return ['code'=>0,'msg'=>$res['msg']];
			} else if ($res['code']==0) {
				return ['code'=>1,'msg'=>$res['msg']];
			}
		} else {
			return $this->fetch();
		}
	}
}
